﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Webshop20.Domain.Model.Customers;
using Webshop20.Factories.Contracts;
using Tools.Common.ServiceLocator;
using Web.App.Infrastructure.ApplicationMessages.Contracts;
using Tools.Domain.Specifications.Constraints;

namespace Webshop20.Factories.Implementation
{
    public class CustomerFactory : ICustomerFactory 
    {
        private IServiceLocator serviceLocator;
        private IDomainMessage customerDomainMessage;

        public CustomerFactory(IServiceLocator serviceLocator)
        {
            this.serviceLocator = serviceLocator;
        }
        
        public Customer Create(string eMail, string firstName, string lastName, Callback callback)
        {
            var customerConstraintSet = serviceLocator.Resolve<IConstraintSet<Customer>>();
            Customer newCustomer = new Customer(customerConstraintSet);
       
            newCustomer.SetCustomerData(firstName, lastName, eMail, string.Empty, string.Empty, string.Empty,string.Empty, CustomerFactoryCallback);

            if (customerDomainMessage != null)
            {
                callback(customerDomainMessage);
                return null;
            }

            return newCustomer;
        }

        internal void CustomerFactoryCallback(IDomainMessage domainMessage)
        {
            customerDomainMessage = domainMessage;
        }
    }
}
